package com.kitwee.serialport.data.model;


import android.os.Parcel;
import android.os.Parcelable;

public class SerialPortParam implements Parcelable {

    private String devicePath = null; // 串口设备路径

    private int baudRate = -1; // 波特率

    private Parity parity = Parity.None; // 校验方式

    private Databits databits = Databits.Eight; // 数据位

    private Stopbits stopbits = Stopbits.One; // 停止位

    private SerialPortParam() {
    }

    private SerialPortParam(SerialPortParam param) {
        this.devicePath = param.getDevicePath();
        this.baudRate = param.getBaudRate();
        this.parity = param.getParity();
        this.databits = param.getDatabits();
        this.stopbits = param.getStopbits();
    }

    public String getDevicePath() {
        return devicePath;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public Parity getParity() {
        return parity;
    }

    public Databits getDatabits() {
        return databits;
    }

    public Stopbits getStopbits() {
        return stopbits;
    }

    public static class Builder {

        private SerialPortParam param;

        public Builder() {
            param = new SerialPortParam();
        }

        public Builder devicePath(String devicePath) {
            param.devicePath = devicePath;
            return this;
        }

        public Builder baudRate(int baudRate) {
            param.baudRate = baudRate;
            return this;
        }

        public Builder baudRate(String baudRate) {
            param.baudRate = Integer.valueOf(baudRate);
            return this;
        }

        public Builder parity(Parity parity) {
            param.parity = parity;
            return this;
        }

        public Builder parity(String parity) {
            param.parity = Parity.fromValue(parity);
            return this;
        }

        public Builder databits(Databits databits) {
            param.databits = databits;
            return this;
        }

        public Builder databits(String databits) {
            param.databits = Databits.fromValue(Integer.valueOf(databits));
            return this;
        }

        public Builder stopbits(Stopbits stopbits) {
            param.stopbits = stopbits;
            return this;
        }

        public Builder stopbits(String stopbits) {
            param.stopbits = Stopbits.fromValue(Integer.valueOf(stopbits));
            return this;
        }

        public SerialPortParam build() {
            return new SerialPortParam(param);
        }

    }

    protected SerialPortParam(Parcel in) {
        devicePath = in.readString();
        baudRate = in.readInt();
        parity = Parity.fromValue(in.readString());
        databits = Databits.fromValue(in.readInt());
        stopbits = Stopbits.fromValue(in.readInt());
    }

    public static final Creator<SerialPortParam> CREATOR = new Creator<SerialPortParam>() {
        @Override
        public SerialPortParam createFromParcel(Parcel in) {
            return new SerialPortParam(in);
        }

        @Override
        public SerialPortParam[] newArray(int size) {
            return new SerialPortParam[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(devicePath);
        parcel.writeInt(baudRate);
        parcel.writeString(String.valueOf(parity.value()));
        parcel.writeInt(databits.value());
        parcel.writeInt(stopbits.value());
    }


}
